<template>
  <el-dialog :title="company.companyName + '--卡规则设置'" :close-on-click-modal="false" @closed="close()" :visible.sync="visible" width="450px">
    <el-form :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :inline="true"
      label-width="100px"
      class="vip-card-rule"
    >
      <h2>积分卡规则</h2>
        <el-form-item prop="pointsAmount">
          1积分抵扣
          <el-input v-model="dataForm.pointsAmount" class="input"></el-input>
          元
        </el-form-item>
      <!-- <h2>充值卡规则</h2>
      <el-form-item prop="giveAmount">
        充值1元赠送
        <el-input v-model="dataForm.giveAmount" placeholder="不赠送请输入0" class="input"></el-input>
        元
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    company: {
      type: Object,
      default: function() {
        return {
          id: null,
          companyName: ''
        };
      }
    }
  },
  data() {
    return {
      visible: false,
      disabled: false,
      dataForm: {
        id: null,
        companyId: this.company.id,
        pointsAmount: null,
        giveAmount: null
      },
      dataRule: {
        pointsAmount: [{ required: true, message: '1积分抵扣多少元不能为空', trigger: 'blur' }],
        giveAmount: [{ required: false, message: '充值1元赠送多少元不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init() {
      this.dataForm.companyId = this.company.id;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.companyId) {
          this.$http({
            url: this.$http.adornUrl(`/member/vipCard/rule/detail/${this.dataForm.companyId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.item.id;
              this.dataForm.pointsAmount = data.item.pointsAmount;
              this.dataForm.giveAmount = data.item.giveAmount;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.disabled = true;
          this.$http({
            url: this.$http.adornUrl(`/member/vipCard/rule/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              companyId: this.dataForm.companyId,
              pointsAmount: this.dataForm.pointsAmount,
              giveAmount: this.dataForm.giveAmount
            })
          }).then(({ data }) => {
            this.disabled = false;
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.close();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    close() {
      this.visible = false;
      this.$emit('close');
    }
  }
};
</script>
<style scoped lang="scss">
  .vip-card-rule {
    .input {
      width: 130px;
    }
  }
</style>
